home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Snippets / SuperSplash 1.0d1 / sources / •AZN_DEBUG / azn_dbug.c next >
Text File  |  1995-10-23  |  3KB  |  154 lines

  1. /*
  2. *    AZN_DBUG.C
  3. *
  4. *    Utility 'C' functions for debugging apps
  5. *    © Andrew Nemeth, Warrimoo Australia, 1995
  6. *    aznemeng@zeta.org.au
  7. *
  8. *    File Created:        6 Mar 95
  9. *    File Ammended:        6, 7, 11, 12, 21 Mar;
  10. *                    3 Jun;
  11. *                    7, 23 Oct 95.
  12. */
  13.  
  14.  
  15. #include    "AZN_DBUG.H"                                    //    debugging utilites
  16.  
  17. #ifndef    NDEBUG
  18.  
  19.  
  20. #include    <new.h>                                        //    ANSI operator 'new' errors
  21. #include    <stdio.h>                                        //    ANSI sprintf() & file functions
  22. #include    <stdlib.h>                                    //    ANSI exit()
  23. #include    <string.h>                                    //    ANSI memset()
  24. #include    <time.h>                                        //    ANSI date & time
  25.  
  26. #ifdef     __MWERKS__
  27. //                                                        Mac Metrowerks SIOUX i/o
  28. #include    <sioux.h>
  29. #endif
  30.  
  31.  
  32. //    USEFUL DEFINES
  33. //                                                        use for low-level MAC debugger
  34. //#define    MAC_LOW_LEVEL
  35.  
  36.  
  37.  
  38. //    FILE FUNCTIONS
  39. //
  40. static void            noMoreMem        ( void );
  41.  
  42.  
  43.  
  44.  
  45. void                    INIT_DEBUG ( void )
  46. //
  47. //    set the function ptr for
  48. //    global operator new failures
  49. //
  50. {
  51.     set_new_handler( noMoreMem );
  52.  
  53. #ifdef __SIOUX__
  54. //                                                        Set sioux display options
  55.     SIOUXSettings.asktosaveonclose = FALSE;
  56.     SIOUXSettings.columns = 40;
  57.     SIOUXSettings.rows = 2;
  58. #endif
  59. }
  60.  
  61.  
  62.  
  63. void                    noMoreMem ( void )
  64. //
  65. //    Signal memory failure
  66. //
  67. {
  68. #ifdef MAC_LOW_LEVEL
  69.     DebugStr( "\pAZN•Operator New failed!" );
  70. #else
  71.     printf( "%s", "AZN•Operator New failed!" );
  72.     exit( 0 );
  73. #endif
  74. }
  75.  
  76.  
  77.  
  78. void                    myAlert ( char * txtFile, unsigned ushLine )
  79. //
  80. //    Signal assertion failure
  81. //
  82. {
  83. #ifdef MAC_LOW_LEVEL
  84.     char        txtPrompt[256] = { "\0" };
  85.  
  86.     sprintf( txtPrompt, "~Assertion Failed: %s, line %u", txtFile, ushLine );
  87. //    note the above is a trick!  Use '~' as first char
  88. //    as this is ASCII 126.  When we come to typecase the
  89. //    char to a 'StringPtr' below, it will fool DebugStr into
  90. //    thinking that it has a pascal string 126 chars long!
  91. //                    
  92.     DebugStr( (StringPtr)&txtPrompt[0] );
  93. #else
  94.     printf( "Assertion Failed: %s, line %u", txtFile, ushLine );
  95.     exit( 0 );
  96. #endif
  97. }
  98.  
  99.  
  100.  
  101.  
  102. void                    SALT_MEMORY ( void * vptrBuff, long lgSize )
  103. //
  104. //    Salt deadly value into a block of memory.
  105. //    Value used is '0xA3', which if de-referenced
  106. //    by accident will play merry havoc (on Macs!)
  107. //    See Steve Maguire "Writing Solid Code" p.49
  108. //
  109. {
  110.     memset( vptrBuff, 0xA3, size_t(lgSize) );    
  111. }
  112.  
  113.  
  114.  
  115. void                    myDBG_FileString ( char * txtBlurb, char * txtSourceFile, unsigned ushLineNo )
  116. //
  117. //    Dump the 'txtBlurb' into a debug file.
  118. //    Also dumpts the source file & line number,
  119. //    as well as a date stamp as to when write was done
  120. //    Using ANSI, dump the current date/time 
  121. //    into the char * given
  122. //
  123. //    Format: "Saturday, 21 Oct 1995 at 02:21:00 PM"
  124. //
  125. {
  126.     #define        ktxtFileName        "AZN_DBUG.TXT"
  127.  
  128.     #define        kshBuffSize          50
  129.  
  130.     FILE            * ptrrecFILE;
  131.     time_t        ulgNow;    
  132.     struct tm        * ptrrecDate;
  133.     char            txtMyDate[ kshBuffSize ],
  134.                 txtOutput[ 256 ];
  135.     char            * txtFormat = { "%A, %d %b %Y at %I:%M:%S %p" };
  136.  
  137. //                                                        grab today's date/ time as ANSI struct
  138.     ulgNow = time( NULL );    
  139.     ptrrecDate = localtime( &ulgNow );
  140.     strftime( txtMyDate, kshBuffSize, txtFormat, ptrrecDate );
  141. //                                                        open debug file for writing
  142.     ptrrecFILE = fopen( ktxtFileName, "a" );
  143. //                                                        output user's blurb
  144.     fputc( '\n', ptrrecFILE );
  145.     sprintf( txtOutput, "\"%s\"\nSource:  %s\nLine:    %u\nAt:      %s\n", \
  146.             txtBlurb, txtSourceFile, ushLineNo, txtMyDate );
  147.  
  148.     fputs( txtOutput, ptrrecFILE );
  149. //                                                        close debug file
  150.     fclose( ptrrecFILE );
  151. }
  152.  
  153. #endif
  154.